import Taro from '@tarojs/taro'

// const domain = 'https://chat.yanjinglong.com'
const domain = 'https://www.jiayidashixiong.cn' //'https://www.jiayidashixiong.cn' // 'http://192.168.0.105:8080' // 'http://localhost:8080'

const interceptor = function(chain) {
  const { requestParams } = chain
  const { url, header, loading } = requestParams
  if (loading) {
    Taro.showLoading()
  }
  const token = Taro.getStorageSync('token')
  requestParams.url = domain + url
  requestParams.header = {
    ...header,
    ...(token ? { token } : {})
  }
  return chain.proceed(requestParams).then(res => {
    if (requestParams.loading) {
      Taro.hideLoading()
    }

    const { data, msg, code } = res.data || {}
    return {
      data,
      msg,
      code,
      success: code === 0
    }
  })
}

Taro.addInterceptor(interceptor)